<?php

namespace framework\support;

use framework\third_party\Medoo;
use PDO;




class Db
{
    private static $_instance = null;
    private static  $database = null;
    /**
     * Single constructor. 私有的构造方法
     */
    private function __construct()
    {;
    }

    /**
     * @purpose: 创建__clone方法防止对象被复制克隆
     */
    private function __clone()
    {
        // E_USER_ERROR只能通过trigger_error($msg, E_USER_ERROR)手动触发。
        // E_USER_ERROR是用户自定义错误类型，可以被set_error_handler错误处理函数捕获，允许程序继续运行。
        // E_ERROR是系统错误，不能被set_error_handler错误处理函数捕获，程序会退出运行
        trigger_error('Clone is not allow!', E_USER_ERROR);
    }


    /**
     * @return Single|Object 单例方法,用于访问实例的公共的静态方法
     */
    public static function getInstance()
    {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self;
            $host = getenv('DB_HOST') ?? '127.0.0.1';
            $port = getenv('DB_PORT') ?? '3306';
            $name = getenv('DB_NAME');
            $charset = getenv('DB_CHARSET') ?? 'utf-8';
            $user = getenv('DB_USER') ?? 'root';
            $prefix = getenv('DB_PREFIX') ?? '';
            $password = getenv('DB_PASSWORD') ?? '';
            $driver = getenv('DB_DRIVER') ?? 'mysql';
            if ($host) {
                self::$database = new Medoo([
                    // required
                    'database_type' => $driver,
                    'database_name' => $name,
                    'server' => $host,
                    'username' => $user,
                    'password' => $password,

                    // [optional]
                    'charset' => $charset,
                    'port' => $port,

                    // [optional] Table prefix
                    'prefix' => $prefix,

                    // [optional] driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
                    'option' => [
                        PDO::ATTR_CASE => PDO::CASE_NATURAL
                    ],
                    // [optional] Medoo will execute those commands after connected to the database for initialization
                    'command' => [
                        'SET SQL_MODE=ANSI_QUOTES'
                    ]
                ]);
            }
        }
        return self::$_instance;
    }

    public static function getDatabase()
    {
        return self::$database;
    }

    public static function select()
    {
        $args = func_get_args();
        return self::$database->select(...$args);
    }

    public static function insert()
    {
        $args = func_get_args();
        return self::$database->insert(...$args);
    }

    public static function update()
    {
        $args = func_get_args();
        return self::$database->update(...$args);
    }

    public static function delete()
    {
        $args = func_get_args();
        return self::$database->delete(...$args);
    }

    public static function replace()
    {
        $args = func_get_args();
        return self::$database->replace(...$args);
    }

    public static function get()
    {
        $args = func_get_args();
        return self::$database->get(...$args);
    }

    public static function has()
    {
        $args = func_get_args();
        return self::$database->has(...$args);
    }

    public static function rand()
    {
        $args = func_get_args();
        return self::$database->rand(...$args);
    }
    public static function count()
    {
        $args = func_get_args();
        return self::$database->count(...$args);
    }
    public static function max()
    {
        $args = func_get_args();
        return self::$database->max(...$args);
    }
    public static function min()
    {
        $args = func_get_args();
        return self::$database->min(...$args);
    }
    public static function avg()
    {
        $args = func_get_args();
        return self::$database->avg(...$args);
    }
    public static function sum()
    {
        $args = func_get_args();
        return self::$database->sum(...$args);
    }
    public static function id()
    {
        $args = func_get_args();
        return self::$database->id(...$args);
    }
    public static function action()
    {
        $args = func_get_args();
        return self::$database->action(...$args);
    }
    public static function query($query)
    {
        return self::$database->query($query);
    }

    public static function quote($string)
    {
        return self::$database->quote($string);
    }
    public static function raw()
    {
        $args = func_get_args();
        return Medoo::raw(...$args);
    }
    public static function debug()
    {
        return self::$database->debug();
    }
    public static function error()
    {
        return self::$database->error();
    }
    public static function log()
    {
        return self::$database->log();
    }
    public static function last()
    {
        return self::$database->last();
    }
    public static function info()
    {
        return self::$database->info();
    }
    public static function pdo()
    {
        return self::$database->pdo;
    }
}
